<template>
  <el-dialog v-el-drag-dialog :title="!form.roleId?'新建规则':'编辑规则'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="140px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="地址" prop="url">
          <el-input v-model="form.url" />
        </el-form-item>
        <el-form-item label="代理" prop="proxy">
          <el-radio v-for="item in optionOfProxy" :key="'proxy_' + item.dictLabel" v-model="form.proxy" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
        </el-form-item>
        <el-form-item label="搜索条件" prop="paths">
          <el-input v-model="form.paths" />
        </el-form-item>
        <el-form-item label="标题表达式" prop="titleXpath">
          <el-input v-model="form.titleXpath" />
        </el-form-item>
        <el-form-item label="分组表达式" prop="groupXpath">
          <el-input v-model="form.groupXpath" />
        </el-form-item>
        <el-form-item label="大小表达式" prop="sizeXpath">
          <el-input v-model="form.sizeXpath" />
        </el-form-item>
        <el-form-item label="日期表达式" prop="dateXpath">
          <el-input v-model="form.dateXpath" />
        </el-form-item>
        <el-form-item label="配置详情页" prop="detail">
          <el-radio v-for="item in optionOfConfig" :key="'proxy_' + item.dictLabel" v-model="form.detail" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
        </el-form-item>
        <el-form-item label="详情页根元素表达式" prop="rootXpath">
          <el-input v-model="form.rootXpath" />
        </el-form-item>
        <el-form-item label="磁力地址表达式" prop="magnetXpath">
          <el-input v-model="form.magnetXpath" />
        </el-form-item>
        <el-form-item label="提取码" prop="accessCode">
          <el-input v-model="form.accessCode" />
        </el-form-item>
        <el-form-item label="文件表达式" prop="filesXpath">
          <el-input v-model="form.filesXpath" />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { magnetLoad, magnetSave } from '@/api/tool/search'

export default {
  name: 'RuleFormDialog',
  data() {
    const validateUniq = (rule, value, callback) => {
      if (value === undefined || value === '') {
        rule.message = '该项不能为空'
        return callback(new Error(rule.message))
      }

      const param = rule.param.replaceParam
      rule.param[param] = value
      rule.param.id = this.form.id
      this.$http.get(rule.url, {
        params: rule.param
      }).then(response => {
        if (response.valid) {
          callback()
        } else {
          callback(new Error(rule.message))
        }
      }).catch(() => {
        callback()
      })
    }

    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        name: undefined,
        url: undefined,
        proxy: undefined,
        paths: undefined,
        titleXpath: undefined,
        groupXpath: undefined,
        sizeXpath: undefined,
        dateXpath: undefined,
        detail: undefined,
        rootXpath: undefined,
        magnetXpath: undefined,
        accessCode: undefined,
        filesXpath: undefined
      },
      rules: {
        name: [{
          required: true,
          trigger: 'blur',
          param: {
            replaceParam: 'name'
          },
          validator: validateUniq,
          url: `${process.env.VUE_APP_MODULE_ADMIN_API}/api/search/magnet/rule/checkUnique`,
          message: '已存在规则名称'
        }],
        url: [
          { required: true, message: '规则URL不能为空' },
          {
            trigger: 'blur',
            param: {
              replaceParam: 'url'
            },
            validator: validateUniq,
            url: `${process.env.VUE_APP_MODULE_ADMIN_API}/api/search/magnet/rule/checkUnique`,
            message: '已存在规则URL'
          }]
      },
      optionOfProxy: [],
      optionOfConfig: []
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
        this.getDictData()
      })
    },
    loadForm(id) {
      magnetLoad({ 'id': id }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['spider:rule:edit'])
      }).catch(() => {})
    },
    getDictData() {
      this.$dictGroup('sys_available_type').then(response => {
        this.optionOfProxy = response
        this.optionOfConfig = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          magnetSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.ruleForm.resetFields()
      this.visible = false
    }
  }
}
</script>
